package cc.higou.core.controller;

import cc.higou.core.pojo.Product;
import cc.higou.core.pojo.SuperPojo;
import cc.higou.service.ProductService;
import cc.higou.service.SkuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 说明：
 * 商品详细信息的控制器
 *
 * @author:dongxing
 * @time:--
 */
@Controller
public class ProductController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;

    @Autowired
    private SkuService skuService;

    @RequestMapping(value = "/product/detail")
    public String ProductSearch(Long productId, Model model) {
        LOGGER.info("ProductController类中的ProductSearch方法执行了");
        //根据商品的id去查询相关的商品

        //先查询商品的信息
        Product product = productService.findProductById(productId);
        //查询库存的信息(SkuService中含有库存信息和颜色的信息)
        List<SuperPojo> skus = skuService.findSkuByProductId(productId);

        SuperPojo superPojo = new SuperPojo();
        superPojo.setProperty("product", product);
        superPojo.setProperty("skus", skus);

        Map<Long, String> colors = new TreeMap<>();

        //排除商品的库存颜色中相同的颜色
        for (SuperPojo pojo : skus) {
            Long id = Long.parseLong(pojo.get("color_id") + "");
            String name = (String) pojo.get("colorName");
            colors.put(id, name);
        }

        superPojo.put("colors", colors);


        model.addAttribute("superPojo", superPojo);


        return "product";
    }
}
